<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>vue项目配置别名 | 爆米花小布</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script defer="defer" async="async" src="//cpro.baidustatic.com/cpro/ui/cm.js"></script>
    <meta name="description" content=" ">
    
    <link rel="preload" href="/assets/css/0.styles.54de4a22.css" as="style"><link rel="preload" href="/assets/js/app.36e8cb25.js" as="script"><link rel="preload" href="/assets/js/2.cd8e88cf.js" as="script"><link rel="preload" href="/assets/js/37.2cbd5350.js" as="script"><link rel="prefetch" href="/assets/js/10.5a175a73.js"><link rel="prefetch" href="/assets/js/11.7c8d5810.js"><link rel="prefetch" href="/assets/js/12.c77aca0d.js"><link rel="prefetch" href="/assets/js/13.fc475cb0.js"><link rel="prefetch" href="/assets/js/14.517ed6e7.js"><link rel="prefetch" href="/assets/js/15.7dc9cc39.js"><link rel="prefetch" href="/assets/js/16.4223237a.js"><link rel="prefetch" href="/assets/js/17.0e3dcd26.js"><link rel="prefetch" href="/assets/js/18.132512c7.js"><link rel="prefetch" href="/assets/js/19.55275db3.js"><link rel="prefetch" href="/assets/js/20.621b52a1.js"><link rel="prefetch" href="/assets/js/21.daa66e46.js"><link rel="prefetch" href="/assets/js/22.537fd259.js"><link rel="prefetch" href="/assets/js/23.81c79725.js"><link rel="prefetch" href="/assets/js/24.ee80a08c.js"><link rel="prefetch" href="/assets/js/25.260c7616.js"><link rel="prefetch" href="/assets/js/26.03e10148.js"><link rel="prefetch" href="/assets/js/27.768e5af1.js"><link rel="prefetch" href="/assets/js/28.ad0f08eb.js"><link rel="prefetch" href="/assets/js/29.9e188b97.js"><link rel="prefetch" href="/assets/js/3.9cba588d.js"><link rel="prefetch" href="/assets/js/30.f2aa9a7c.js"><link rel="prefetch" href="/assets/js/31.93c74095.js"><link rel="prefetch" href="/assets/js/32.4735eb89.js"><link rel="prefetch" href="/assets/js/33.7b05518b.js"><link rel="prefetch" href="/assets/js/34.3db00b39.js"><link rel="prefetch" href="/assets/js/35.ea9a0227.js"><link rel="prefetch" href="/assets/js/36.5873bde7.js"><link rel="prefetch" href="/assets/js/38.6c2de9e4.js"><link rel="prefetch" href="/assets/js/39.d3c0116f.js"><link rel="prefetch" href="/assets/js/4.97aec80c.js"><link rel="prefetch" href="/assets/js/40.47b05f9f.js"><link rel="prefetch" href="/assets/js/41.5dfb852e.js"><link rel="prefetch" href="/assets/js/42.1d7cf4c2.js"><link rel="prefetch" href="/assets/js/43.43f59d94.js"><link rel="prefetch" href="/assets/js/44.19309bf6.js"><link rel="prefetch" href="/assets/js/45.dbee3f0a.js"><link rel="prefetch" href="/assets/js/46.a373745d.js"><link rel="prefetch" href="/assets/js/5.bc4ebf1d.js"><link rel="prefetch" href="/assets/js/6.805bb5f3.js"><link rel="prefetch" href="/assets/js/7.9724c5a3.js"><link rel="prefetch" href="/assets/js/8.6c6d5662.js"><link rel="prefetch" href="/assets/js/9.641efeda.js">
    <link rel="stylesheet" href="/assets/css/0.styles.54de4a22.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">爆米花小布</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/element-ui/el-table-select-all/" class="nav-link">
  前端博客
</a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-element-dict/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  PC端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-vant-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  移动端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/element-ui/el-table-select-all/" class="nav-link">
  前端博客
</a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-element-dict/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  PC端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://shenxiaobu.github.io/vue-vant-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  移动端字典插件包
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Element-ui组件使用经验</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/element-ui/el-table-select-all/" class="sidebar-link">使用el-table实现全选功能</a></li><li><a href="/element-ui/el-table-sku/" class="sidebar-link">利用element实现sku规格相关功能</a></li><li><a href="/element-ui/el-cropper/" class="sidebar-link">利用vue-cropper实现图片裁剪功能</a></li><li><a href="/element-ui/el-form-nesting/" class="sidebar-link">el-form嵌套表单校验</a></li><li><a href="/element-ui/el-theme/" class="sidebar-link">使用element-ui组件库实现主题切换功能</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>JS必备知识</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/JS/call-apply-bind/" class="sidebar-link">js中的call、apply和bind</a></li><li><a href="/JS/debounce-throttle/" class="sidebar-link">JS中防抖与节流的讲解与封装</a></li><li><a href="/JS/tree-data/" class="sidebar-link">JS中树形结构数据相关方法</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS实战经验</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/CSS/contour-layout/" class="sidebar-link">css实现等高布局</a></li><li><a href="/CSS/fixed-aspect-ratio/" class="sidebar-link">css实现固定宽高比</a></li><li><a href="/CSS/draw-triangle/" class="sidebar-link">CSS画一个三角形</a></li><li><a href="/CSS/draw-ring/" class="sidebar-link">CSS画一个环形进度条</a></li><li><a href="/CSS/pseudo-class/" class="sidebar-link">伪类和伪元素的区别与使用</a></li><li><a href="/CSS/clear-float/" class="sidebar-link">清除浮动的原理与时机</a></li><li><a href="/CSS/layout/" class="sidebar-link">三栏布局问题（左右固定宽度 中间自适应）</a></li><li><a href="/CSS/flex/" class="sidebar-link">flex布局祥解</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>实用的npm包</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/npm/xiaobu-pic-zoom/" class="sidebar-link">xiaobu-pic-zoom图片放大镜</a></li><li><a href="/npm/vue-message-code/" class="sidebar-link">vue-message-code短信验证码</a></li><li><a href="/npm/xiaobu-water-marker/" class="sidebar-link">xiaobu-water-marker水印</a></li><li><a href="/npm/vue-flutter/" class="sidebar-link">vue-flutter漂浮动画</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>VUE小妙招</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-config/search-menu/" class="sidebar-link">利用fuse.js实现搜索菜单功能</a></li><li><a href="/vue-config/alias/" aria-current="page" class="active sidebar-link">vue项目配置别名</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vue-config/alias/#配置alias" class="sidebar-link">配置alias</a></li><li class="sidebar-sub-header"><a href="/vue-config/alias/#path-join-和-path-resolve-的区别" class="sidebar-link">path.join() 和 path.resolve() 的区别</a></li></ul></li><li><a href="/vue-config/svg-sprite-loader/" class="sidebar-link">vue项目配置使用svg图标</a></li><li><a href="/vue-config/vue-mode-env/" class="sidebar-link">vue项目的模式和环境变量</a></li><li><a href="/vue-config/system-config/" class="sidebar-link">vue项目配置系统变量</a></li><li><a href="/vue-config/axios-optimization-one/" class="sidebar-link">vue项目axios请求优化（一）</a></li><li><a href="/vue-config/axios-optimization-two/" class="sidebar-link">vue项目axios请求优化（二）</a></li><li><a href="/vue-config/axios-optimization-three/" class="sidebar-link">vue项目axios请求优化（三）</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>前端工具</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/tool/use-nvm/" class="sidebar-link">nvm的使用</a></li><li><a href="/tool/vscode/sftp/" class="sidebar-link">vscode插件之sftp</a></li><li><a href="/tool/vscode/koroFileHeader/" class="sidebar-link">vscode插件之koroFileHeader</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="vue项目配置别名"><a href="#vue项目配置别名" class="header-anchor">#</a> vue项目配置别名</h1> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>项目配置别名后，将使引入文件更加快捷方便。</p></div> <h2 id="配置alias"><a href="#配置alias" class="header-anchor">#</a> 配置alias</h2> <p>通常我们的项目结构如下所示</p> <blockquote><p>src</p> <blockquote><p>api ...</p> <p>assets ...</p> <p>components ...</p> <p>store ...</p> <p>utils ...</p></blockquote></blockquote> <p>然后每次需要引入某文件的时候都得 src/api ... 或 src/components ...  等</p> <p>这无疑是非常麻烦的，于是就可以配置别名，来避免我们书写的麻烦</p> <p>向前看，就不举vue-cli3.0之前版本是在哪个文件配置的了，直接以vue-cli3.0为例</p> <p>在vue.config.js上做如下配置</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//vue.config.js</span>
<span class="token comment">//引入node的path模块</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span>
<span class="token comment">//__dirname 当前模块的目录名</span>
<span class="token comment">//path.join() 方法会将所有给定的 path 片段连接到一起（使用平台特定的分隔符作为定界符），然后规范化生成的路径。</span>
<span class="token keyword">const</span> <span class="token function-variable function">resolve</span> <span class="token operator">=</span> <span class="token parameter">dir</span> <span class="token operator">=&gt;</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> dir<span class="token punctuation">)</span>

<span class="token comment">//如果path模块仅在此次用到，也可写成如下代码（不推荐）</span>
<span class="token keyword">const</span> <span class="token function-variable function">resolve</span> <span class="token operator">=</span> <span class="token parameter">dir</span> <span class="token operator">=&gt;</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> dir<span class="token punctuation">)</span>

module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">configureWebpack</span><span class="token operator">:</span><span class="token punctuation">{</span>
        <span class="token literal-property property">resolve</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token literal-property property">alias</span><span class="token operator">:</span><span class="token punctuation">{</span>
                <span class="token string-property property">'@'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@api'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/api'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@components'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/components'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@assets'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/assets'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@store'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/store'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@utils'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/utils'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>有时候配置文件还可以这样配置</p> <div class="language-js extra-class"><pre class="language-js"><code> <span class="token function-variable function">configureWebpack</span><span class="token operator">:</span> <span class="token parameter">config</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
 		Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>config<span class="token punctuation">,</span><span class="token punctuation">{</span>
 			<span class="token literal-property property">resolve</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token literal-property property">alias</span><span class="token operator">:</span><span class="token punctuation">{</span>
                    <span class="token string-property property">'@'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    <span class="token string-property property">'@api'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/api'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    <span class="token string-property property">'@components'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/components'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    <span class="token string-property property">'@assets'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/assets'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    <span class="token string-property property">'@store'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/store'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    <span class="token string-property property">'@utils'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/utils'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
 		<span class="token punctuation">}</span><span class="token punctuation">)</span>
   	<span class="token punctuation">}</span>
</code></pre></div><p>config就是指向configureWebpack，所以只要确保 configureWebpack.resolve.alias或者</p> <p>chainWebpack..resolve.alias有配置到即可。方式可以多种多样，比如</p> <div class="language-js extra-class"><pre class="language-js"><code>    <span class="token function-variable function">chainWebpack</span><span class="token operator">:</span> <span class="token parameter">config</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      config<span class="token punctuation">.</span>resolve<span class="token punctuation">.</span>alias
        <span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'@'</span><span class="token punctuation">,</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'@api'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/api'</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'@components'</span><span class="token punctuation">,</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/components'</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'@assets'</span><span class="token punctuation">,</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/assets'</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'@store'</span><span class="token punctuation">,</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/store'</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'@utils'</span><span class="token punctuation">,</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/utils'</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre></div><p>chainWebpack为链式配置</p> <p>配置完之后就可以如下方式引用文件</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token constant">XXX</span> <span class="token keyword">from</span> <span class="token string">'@api/...'</span>
<span class="token keyword">import</span> <span class="token constant">XXX</span> <span class="token keyword">from</span> <span class="token string">'@components/...'</span>
<span class="token keyword">import</span> <span class="token constant">XXX</span> <span class="token keyword">from</span> <span class="token string">'@utils/...'</span>
</code></pre></div><p>当然肯定有人也会发现，有的项目配置别名是采用 path.resolve(),配置如下</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//vue.config.js</span>
<span class="token comment">//引入node的path模块</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span>
<span class="token comment">//__dirname 当前模块的目录名</span>
<span class="token comment">//path.resolve() 方法会将路径或路径片段的序列解析为绝对路径。</span>
<span class="token keyword">const</span> <span class="token function-variable function">resolve</span> <span class="token operator">=</span> <span class="token parameter">dir</span> <span class="token operator">=&gt;</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> dir<span class="token punctuation">)</span>

<span class="token comment">//如果path模块仅在此次用到，也可写成如下代码（不推荐）</span>
<span class="token keyword">const</span> <span class="token function-variable function">resolve</span> <span class="token operator">=</span> <span class="token parameter">dir</span> <span class="token operator">=&gt;</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> dir<span class="token punctuation">)</span>

module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">configureWebpack</span><span class="token operator">:</span><span class="token punctuation">{</span>
        <span class="token literal-property property">resolve</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token literal-property property">alias</span><span class="token operator">:</span><span class="token punctuation">{</span>
                <span class="token string-property property">'@'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@api'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/api'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@components'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/components'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@assets'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/assets'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@store'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/store'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string-property property">'@utils'</span><span class="token operator">:</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string">'src/utils'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>咋一看怎么完全一样？</p> <!----> <p>其实不然，接下来讲讲 path.join() 和 path.resolve() 的区别</p> <h2 id="path-join-和-path-resolve-的区别"><a href="#path-join-和-path-resolve-的区别" class="header-anchor">#</a> path.join() 和 path.resolve() 的区别</h2> <p>新建个path.js文件</p> <p>文件内容如下</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;__dirname   //&quot;</span> <span class="token operator">+</span> __dirname<span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve()   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname)   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'src')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'src'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'/src')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'src/ab')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'src/ab'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'/src/ab')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src/ab'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'/src/ab','..')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'src/ab'</span><span class="token punctuation">,</span><span class="token string">'..'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'/src/ab','..')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src/ab'</span><span class="token punctuation">,</span><span class="token string">'..'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.resolve(__dirname,'/src/ab','..','c')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src/ab'</span><span class="token punctuation">,</span><span class="token string">'..'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>


console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join()   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname)   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'src')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'src'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'/src')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'src/ab')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'src/ab'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'/src/ab')  //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src/ab'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'src/ab','..')   //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'src/ab'</span><span class="token punctuation">,</span><span class="token string">'..'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'/src/ab','..')  //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src/ab'</span><span class="token punctuation">,</span><span class="token string">'..'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;path.join(__dirname,'/src/ab','..','c')  //&quot;</span> <span class="token operator">+</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span><span class="token string">'/src/ab'</span><span class="token punctuation">,</span><span class="token string">'..'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

</code></pre></div><p>使用node执行该命令，打印结果如下</p> <div class="language-shell extra-class"><pre class="language-shell"><code>__dirname   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node
path.resolve<span class="token punctuation">(</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node
path.resolve<span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'src'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'/src'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>src
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'src/ab'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src<span class="token punctuation">\</span>ab
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>src<span class="token punctuation">\</span>ab
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span>,<span class="token string">'..'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span>,<span class="token string">'..'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>src
path.resolve<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span>,<span class="token string">'..'</span>,<span class="token string">'c'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>src<span class="token punctuation">\</span>c
path.join<span class="token punctuation">(</span><span class="token punctuation">)</span>   //.
path.join<span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'src'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'/src'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'src/ab'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src<span class="token punctuation">\</span>ab
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span><span class="token punctuation">)</span>  //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src<span class="token punctuation">\</span>ab
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'src/ab'</span>,<span class="token string">'..'</span><span class="token punctuation">)</span>   //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span>,<span class="token string">'..'</span><span class="token punctuation">)</span>  //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src
path.join<span class="token punctuation">(</span>__dirname,<span class="token string">'/src/ab'</span>,<span class="token string">'..'</span>,<span class="token string">'c'</span><span class="token punctuation">)</span>  //D:<span class="token punctuation">\</span>a<span class="token punctuation">\</span>b<span class="token punctuation">\</span>node<span class="token punctuation">\</span>src<span class="token punctuation">\</span>c

</code></pre></div><h3 id="path-resolve-方法"><a href="#path-resolve-方法" class="header-anchor">#</a> <strong>path.resolve()方法</strong></h3> <p>path.resolve()方法是以程序为根目录，作为起点，根据参数解析出一个绝对路径</p> <p><strong>以应用程序为根目录</strong></p> <p><strong>普通字符串代表子目录</strong></p> <p><strong>/代表绝对路径根目录</strong></p> <p><strong>..代表上级</strong></p> <p>path.resolve(__dirname,'/src')   //D:\src   /指向跟目录</p> <p>path.resolve(__dirname,'/src/ab','..','c')   //D:\src\c   ..代表上级目录</p> <h3 id="path-join-方法"><a href="#path-join-方法" class="header-anchor">#</a> <strong>path.join()方法</strong></h3> <p>path.join()方法是将多个参数字符串合并成一个路径字符串</p> <p>path.join(__dirname,'/src')   //D:\a\b\node\src   /不影响拼接</p> <p>path.join(__dirname,'/src/ab','..','c')  //D:\a\b\node\src\c    ..代表上级目录</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/vue-config/search-menu/" class="prev">
        利用fuse.js实现搜索菜单功能
      </a></span> <span class="next"><a href="/vue-config/svg-sprite-loader/">
        vue项目配置使用svg图标
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><div id="loading-mask"><div class="loading-wrapper"><span class="loading-dot loading-dot-spin"><i></i> <i></i> <i></i> <i></i></span></div></div><!----></div></div>
    <script src="/assets/js/app.36e8cb25.js" defer></script><script src="/assets/js/2.cd8e88cf.js" defer></script><script src="/assets/js/37.2cbd5350.js" defer></script>
  </body>
</html>
